package softswitch.service;

import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import softswitch.model.Circuit;
import softswitch.model.Plan;


@Stateless
public class CircuitDAO {
	
	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	
	
	public Circuit getById(Integer id){	
		return em.find(Circuit.class, id);
	}
    
    public Circuit save(Circuit circuit) {
    	em.persist(circuit); 
    	return circuit;
    }
    
    public Circuit update(Circuit circuit) {
    	return em.merge(circuit); 
    }
    
    public void delete(Integer id) {
    	em.remove(em.find(Circuit.class, id));	
    }
    
	@SuppressWarnings("unchecked")
    public List<Circuit> list(Date activationStartdate, Date activationEndate, Boolean enabled) {    	
	  	String sql = "SELECT e FROM Circuit e WHERE e.calldate BETWEEN :startdate AND :enddate";
	  	if(enabled!=null){
	  		sql = sql + " AND e.enabled=:enabled ";
	  	}
		Query query = em.createQuery(sql);
		query.setParameter("startdate", activationStartdate);
		query.setParameter("enddate", activationEndate);
	  	if(enabled!=null){
	  		query.setParameter("enabled", enabled);
	  	}		
		try{
			return query.getResultList();
		}
		catch(Exception e){
			return null;
		}
    }
    
   // ------------------------------------------------------------------------------
    
	@SuppressWarnings("unchecked")
    public List<Circuit> list(int first, int max) {    	
    	Query query = em.createQuery("SELECT e FROM Circuit e");
    	if (max > 0) {
            query.setMaxResults(max);
        }
        if (first > 0) {
            query.setFirstResult(first);
        }
        return query.getResultList();   
    }
	
	
	public Circuit getByCode(Long code) {
    	Circuit result = null;
		Query query = em.createQuery("SELECT e FROM Circuit e WHERE e.code=:code");
		query.setParameter("code", code);
		try {
			result = (Circuit) query.getSingleResult(); 
        }
    	catch (Exception e) {
			//e.printStackTrace();
		}
        return result; 
    }

	@SuppressWarnings("unchecked")
	public List<Circuit> listByOrganizationId(Integer organization_id) {
    	List<Circuit> results = null;
		Query query = em.createQuery("SELECT e FROM Circuit e WHERE e.organization_id=:organization_id");
		query.setParameter("organization_id", organization_id);
		try {
			results = query.getResultList(); 
        }
    	catch (Exception e) {
			e.printStackTrace();
		}
        return results; 
	}
	
	@SuppressWarnings("unchecked")
	public List<Circuit> listByPlan(Plan plan) {
    	List<Circuit> results = null;
		Query query = em.createQuery("SELECT e FROM Circuit e WHERE e.plan=:plan");
		query.setParameter("plan", plan);
		try {
			results = query.getResultList(); 
        }
    	catch (Exception e) {
			e.printStackTrace();
		}
        return results; 
	}
    public Long getTotal() {    	
    	Query query = em.createQuery("SELECT COUNT(e) FROM Circuit e");
    	return (Long) query.getSingleResult();
    }
    
}

